package cn.jdy.plugins.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.SpringUploadFile;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Member;

/**
 * 会员注册控制类
 *
 * @author wuzhiliang
 */
@Controller
@RequestMapping("/register")
public class RegisterCtrl extends BaseCtrl {

  /**
   * 用户注册页面
   */
  @RequestMapping("")
  public ModelAndView registerIndex() {
    ModelAndView mv = new ModelAndView("/register.jsp");
    return mv;
  }
  
  /**
   * 用户注册页面
   */
  @RequestMapping("/registerMall")
  public ModelAndView registerMall() {
    ModelAndView mv = new ModelAndView("/registerMall.jsp");
    mv.addObject("column", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
    return mv;
  }
	/**
	 * @return /web/register/forSaveMember?username=test&password=123456
	 * @title : 保存注册会员信息
	 * @description: 保存注册信息 跳转到首页或前一个页面
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forSaveMember")
	public ModelAndView forSaveRegisterMember(Member memberBo,String authcode) {
		ModelAndView mv = new ModelAndView();
		if(null == request.getSession().getAttribute(GlobalConstant._PHONECODE)||!authcode.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))){
		  mv.setViewName("/register.jsp");
		  mv.addObject("msg", "短信验证码无效！");
		  return mv;
		}
		  
		try {
			memberBo.setUserType("0");
			memberBo = memberService.saveMember(memberBo);
			memberService.updateMoney(memberBo.getId(), GlobalConstant.SMONEY_REGISTER);
			session.setAttribute("user", memberBo);
			mv.addObject("msg", "success");
			mv.setViewName("/");

		} catch(BaseException e) {
			mv.setViewName("/register.jsp");
			mv.addObject("msg", "注册失败！");
			BaseLog.e(this.getClass(), "保存会员注册信息错误", e);
		}
		return mv;
	}



	/**
	 * @return /
	 * @title : 跳转个人认证资料填写页面
	 * @description: 跳转个人认证资料填写页面
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forPersonalAuthenInit")
	public ModelAndView forPersonalAuthenInit() {
		ModelAndView mv = new ModelAndView();
		try {
			Member menberBo = (Member) super.getMember();
			Member bo = memberService.find(menberBo.getId());
			if("1".equals(bo.getStatus())) {// 已提交申请,请等待审核
				mv.setViewName("forward:/member/myInfo?msg=已提交申请,请等待审核!");
			} else if("1".equals(bo.getProvince())) {//
				if("0".equals(bo.getUserType())) {
					mv.setViewName("forward:/member/myInfo?msg=您个人认证已成功!");
				}
				if("1".equals(bo.getUserType())) {
					mv.setViewName("forward:/member/myInfo?msg=您企业认证已成功!");
				}
			} else {
				mv.addObject("member", bo);
				mv.setViewName("/personalAuthen.jsp");
			}
		} catch(BaseException e) {
			BaseLog.e(this.getClass(), "跳转个人认证资料填写页面失败", e);
		}
		return mv;
	}

	/**
	 * @return /
	 * @title : 跳转企业认证资料填写页面
	 * @description: 跳转企业认证资料填写页面
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forCompanyAuthenInit")
	public ModelAndView forCompanyAuthenInit() {
		ModelAndView mv = new ModelAndView();
		try {
			Member menber = super.getMember(true);
			if("1".equals(menber.getStatus())) {// 已提交申请,请等待审核//if("1".equals(bo.getStatus())){//已提交申请,请等待审核
				mv.setViewName("forward:/member/myInfo?msg=已提交申请,请等待审核!");
			} else if("1".equals(menber.getProvince())) {
				if("0".equals(menber.getUserType())) {
					mv.setViewName("forward:/member/myInfo?msg=您个人认证已成功!");
				}
				if("1".equals(menber.getUserType())) {
					mv.setViewName("forward:/member/myInfo?msg=您企业认证已成功!");
				}
			} else {
				// Company combo =new Company();
				// combo.setUserid(menberBo.getId());
				mv.addObject("member", menber);
				mv.setViewName("/companyAuthen.jsp");
			}
		} catch(BaseException e) {
			BaseLog.e(this.getClass(), "跳转企业认证资料填写页面失败", e);
		}
		return mv;
	}

	/**
	 * @return /
	 * @title : 保存注册会员个人认证信息
	 * @description: 保存注册信息 跳转到首页或前一个页面
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forSavePersonalAuthen")
	public ModelAndView forSavePersonalAuthen(Member member) {
		ModelAndView mv = new ModelAndView();
		try {
			member.setUserType("0");
			member.setApprove("3");
			memberService.updateWithValidator(member);
			mv.addObject("msg", "个人认证申请成功！");
			mv.setViewName("redirect:/");
		} catch(Exception e) {
			mv.setViewName("/personalAuthen.jsp");
			mv.addObject("msg", "个人认证信息失败！");
			BaseLog.e(this.getClass(), "保存注册会员个人认证信息错误", e);
		}
		return mv;
	}

	/**
	 * 上传文件
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/forUpload")
	public ModelAndView forUpload(DefaultMultipartHttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/upfileImg2.jsp");
		try {
			String savePath = "companyScanFile";
			Map<String, String> reMap = SpringUploadFile.upload(request, savePath);
			if(StringUtil.isNullOrEmpty(reMap.get("errorInfo"))) {
				mv.addObject("newUrl", reMap.get("newUrl"));
				mv.addObject("originalName", reMap.get("originalName"));
			} else {
				mv.addObject("errorInfo", reMap.get("errorInfo"));
			}
		} catch(BaseException e) {
			BaseLog.e(this.getClass(), "上传文件预览失败", e);
		}
		return mv;
	}

	/**
	 * @return
	 * @title : 企业认证申请
	 * @description: 企业认证申请
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forSaveMemberAndCompany")
	public ModelAndView forSaveMemberAndCompany(Member member) {
		ModelAndView mv = new ModelAndView();
		try {
			Member entity = super.getMember();
			member.getCompany().setId(entity.getCompany().getId());
			entity.setCompany(member.getCompany());
			entity.setApprove("3");
			memberService.update(entity);
			mv.addObject("msg", "企业认证申请成功");
			mv.setViewName("/");
		} catch(BaseException e) {
			mv.setViewName("/companyAuthen.jsp");
			mv.addObject("msg", "error");
			mv.addObject("member", member);
			BaseLog.e(this.getClass(), "保存企业认证申请信息错误", e);
		}
		return mv;
	}


	/**
	 * @return
	 * @title : 企业注册
	 * @description: 企业注册
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/forRegisterCompany")
	public ModelAndView forRegisterCompany(Member member) {
		ModelAndView mv = new ModelAndView();
		try {
			member.setUserType("1");
			// 添加service层处理事务，保证数据的完整性
			member = memberService.saveMember(member);
			memberService.updateMoney(member.getId(), GlobalConstant.SMONEY_REGISTER);
			session.setAttribute("user", member);

			mv.addObject("msg", "企业注册成功");
			mv.setViewName("/");
		} catch(BaseException e) {
			mv.setViewName("/register.jsp");
			mv.addObject("msg", "error");
			BaseLog.e(this.getClass(), "保存企业注册信息错误", e);
		}
		return mv;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验用户名是否被占用
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkUnickusername")
	@ResponseBody
	public Map<String, Object> checkUnickusername(String param) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(memberService.findcheckUsername(param) > 0) {
			jsonMap.put("status", "n");
			jsonMap.put("info", "用户名已被抢注，请更换一个");
		} else {
			jsonMap.put("status", "y");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验手机号是否被占用
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkUnickmobile")
	@ResponseBody
	public Map<String, Object> checkUnickmobile(String param) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(memberService.findcheckUsermobile(param) > 0) {
			jsonMap.put("status", "n");
			jsonMap.put("info", "该手机号已被注册");
		} else {
			jsonMap.put("status", "y");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验短信验证码
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkAuthcode")
	@ResponseBody
	public Map<String, Object> checkAuthcode(String param) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(param!=null && param.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))) {
			jsonMap.put("status", "y");
		} else {
			jsonMap.put("status", "n");
			jsonMap.put("info", "验证码错误");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: 校验验证码
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkVerifyCode")
	@ResponseBody
	public Map<String, Object> checkVerifyCode(String authcode) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(authcode.equals(request.getSession().getAttribute("verifyCode"))) {
			jsonMap.put("success", "0");
		} else {
			jsonMap.put("success", "1");
		}
		return jsonMap;
	}

	/**
	 * @return
	 * @title
	 * @description: AIAX方式校验短信验证码
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkAuthcodeString")
	@ResponseBody
	public String checkAuthcodeString(String authcode) {
		if(authcode.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))) {
			return "true";
		}
		return "false";
	}

}
